package com.example.videoplayer.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@Component
public class FFmpegUtil {
    private static final Logger logger = LoggerFactory.getLogger(FFmpegUtil.class);

    @PostConstruct
    public void checkFFmpeg() {
        try {
            ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-version");
            pb.redirectErrorStream(true);
            Process process = pb.start();

            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()))) {
                String version = reader.readLine();
                logger.info("FFmpeg version: {}", version);
            }

            int exitCode = process.waitFor();
            if (exitCode != 0) {
                logger.error("FFmpeg check failed with exit code: {}", exitCode);
                throw new RuntimeException("FFmpeg is not properly installed");
            }
        } catch (Exception e) {
            logger.error("Failed to check FFmpeg installation", e);
            throw new RuntimeException("FFmpeg is not properly installed", e);
        }
    }
} 